﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import com.adobe.net.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.loading.*;

    public class RequestCommand extends Command {

		[Inject]
        public var event:RequestEvent;
		[Inject]
        public var loader:LoaderService;
		[Inject]
        public var protocol:ProtocolModel;
		[Inject]
        public var fileInfo:FileInfoModel;
		[Inject]
        public var environment:EnvironmentModel;
		[Inject]
        public var session:SessionModel;

        override public function execute():void{
            var _local1:RequestVO;
            var _local2:String;
            var _local3:Object;
            for each (_local1 in this.event.requests) {
                _local2 = _local1.id;
                _local3 = _local1.params;
                if (this.protocol.hasRequest(_local2)){
                    this.handleRequest(_local1, _local2, _local3);
                } else {
                    this.handleFile(_local1, _local2, _local3);
                };
                if (!_local1.format){
                    _local1.format = ((new URI(_local1.url).getExtension()) || (GameConstants.JSON));
                };
            };
            this.loader.load(this.event.resultHandler, this.event.groupId, this.event.requests, this.event.errorHandler);
        }
        private function handleRequest(_arg1:RequestVO, _arg2:String, _arg3:Object):void{
            var _local5:String;
            _arg1.method = this.protocol.getMethod(_arg2);
            _arg1.pipeId = ((_arg1.pipeId) || (ProtocolConstants.APP_PIPE));
            var _local4:URI = new URI();
            _local4.path = this.protocol.getRequest(_arg2, _arg3);
            if (!Strings.includes(_local4.path, "://")){
                _local5 = (((this.protocol.needsSession(_arg2)) && (this.session.session_id))) ? (this.session.session_id + "/") : "";
                _arg1.url = ((this.environment.main_service_url + _local5) + _local4.path);
            } else {
                _arg1.url = _local4.path;
            };
        }
        private function handleFile(_arg1:RequestVO, _arg2:String, _arg3:Object):void{
            _arg2 = this.protocol.getFile(_arg2, _arg3);
            _arg1.fileSize = this.fileInfo.getBytes(_arg2);
            _arg2 = this.fileInfo.getCdnUrl(_arg2);
            var _local4:int = Numbers.randomIntBetween(1, this.fileInfo.maxSubdomains);
            _arg1.url = Strings.replacePattern(_arg2, "num", _local4.toString());
            if (_arg1.caching){
                _arg1.params = null;
            };
        }

    }
}//package net.wooga.pets.controllers 
